package megajournal.view;

import megajournal.controller.LoginCtrl;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author cska
 * 
 */

public class LoginDlg extends JDialog {

    protected GridBagConstraints c;
    protected JTextField login;
    protected JPasswordField pass;
    protected JButton okButton;
    protected JButton cancelButton;

    protected JLabel l1, l2;

    public LoginDlg(JFrame owner, boolean modal) {
        super(owner,modal);

        setLayout(new GridBagLayout());
        l1 = new JLabel("Пользователь:");
        l1.setHorizontalAlignment(JLabel.RIGHT);
        l2 = new JLabel("Пароль:");
        l2.setHorizontalAlignment(JLabel.RIGHT);
        login = new JTextField(5);
        login.addActionListener(new okButtonClicked(this));
        login.setHorizontalAlignment(JTextField.CENTER);
        pass = new JPasswordField(5);
        pass.addActionListener(new okButtonClicked(this));
        pass.setHorizontalAlignment(JPasswordField.CENTER);
        okButton = new JButton("Войти");
        okButton.addActionListener(new okButtonClicked(this));
        cancelButton = new JButton("Отмена");
        cancelButton.addActionListener(new cancelButtonClicked(this));
        c = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        c.weightx = 0.1;
        add(l1,c);

        c.gridy = 1;
        add(l2,c);

        c.weightx = 0.9;
        c.gridx = 1;
        c.gridy = 0;
        add(login,c);

        c.gridy = 1;
        add(pass,c);

        JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        p.add(okButton);
        p.add(cancelButton);

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 2;
        c.insets = new Insets(0, 0, 0, 0);

        add(p,c);

        setSize(320,140);
        Dimension screen = getToolkit().getScreenSize().getSize();
        setLocation((screen.width-320)/2, (screen.height-140)/2);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setTitle("Вход");
        setResizable(false);
        setVisible(true);
    }

    public String getLogin() {
        return login.getText();
    }

    public String getPass() {
        return pass.getText();
    }

}

class okButtonClicked implements ActionListener {

    protected LoginDlg ld;
    protected LoginCtrl lc;

    public okButtonClicked(LoginDlg ld) {
        this.ld = ld;
        lc = new LoginCtrl(null, null);
    }

    public void actionPerformed(ActionEvent e) {
        lc.setLogin(ld.getLogin());
        lc.setPass(ld.getPass());
        if (lc.canLogin())
            ld.setVisible(false);
        else
            JOptionPane.showMessageDialog(ld, "Не совпадает пользователь и/или пароль", "Ошибка!", JOptionPane.ERROR_MESSAGE);
    }

}

class cancelButtonClicked implements ActionListener {

    protected LoginDlg ld;

    public cancelButtonClicked(LoginDlg ld) {
        this.ld = ld;
    }

    public void actionPerformed(ActionEvent e) {
        ld.setVisible(false);
    }
    
}
